Return home

Click here to return to the main CU psychology R tutorials page.

1: First Install/load ggplot2

#load the package

if (!require(ggplot2)) {
  install.packages(ggplot2)
} else {
  require(ggplot2)
}
## Loading required package: ggplot2

2: Load in Sample Data (NHANES)

Region - Geographic region in the USA: Northeast (1), Midwest (2), South (3), and West (4)

Sex - Biological sex: Male (1), Female (2)

Age - Age measured in months

Urban - Residential population density: Metropolital Area (1), Other (2)

Weight - Weight in pounds

Height - Height in inches

BMI - BMI, measured in kg/(m^2)

nhanes <- read.csv("NHANES1990.csv", stringsAsFactors = F)
nhanes$Age <- nhanes$Age/12
View(nhanes)

3: Scatter Plot & Basic ggplot Syntax

-ggplot() command usage including saving to a variable with a <- ggplot()

-The general format is ggplot(data, aes(x = [x axis variable], y = [y axis variable]) - x and y variables are always specified in this aes() subfunction

Run this line of code

ggplot(nhanes, aes(x = Age, y = Weight))

–axes are set up the way we’d expect, and seem to have sensible values –why is nothing on this graph yet? we haven’t put any graphic actually on the axes yet

We need to tell ggplot() what kind of graphic to put on the axis

A lot of the time, the syntax is geom_[something]

So lets do scatter with geom_point() first:

ggplot(nhanes, aes(x = Age, y = Weight)) + geom_point()

# Wow, lots of data, maybe make the points smaller to see better

ageWeightPlot <- ggplot(nhanes, aes(x = Age, y = Weight)) + geom_point(size = .1, alpha = .3)

ageWeightPlot

# It's a good habbit to save your plots to objects, not just draw them! 

ageWeightPlot <- ggplot(nhanes, aes(x = Age, y = Weight)) + geom_point(size = 1, alpha = .2, aes(color = factor(Region), pch = factor(Region)))

4: Dot Plot Organized by Grouping Factor

Question: what if we want to look at distribution of weights by region in the nhanes data?

With ggplot, if x is a factor (discrete, not continuous) we can plot dots as a function of the fact

nhanes$UrbanRecode <- as.factor(nhanes$Urban)

b <- ggplot(nhanes, aes(x = UrbanRecode, y = Height)) + geom_point()
b

# Woah! So much data, hard to see anything, let's use geom_jitter() with size = .05, width = .1 instead

b <- ggplot(nhanes, aes(x = UrbanRecode, y = Height)) + geom_jitter(size = .05, width = .1)
b

5: Summary Plots

Plotting data points is all well and good, but what if we want to use our plots to summarize distributions? We’ll do that here:

c <- ggplot(nhanes, aes(x = Region, y = Weight)) + stat_summary(fun.y= "mean", geom = "point")
c

# bar plots if you must, and teaching that you can plot overlapping elements -- IN ORDER
d <- ggplot(nhanes, aes(x = Region, y = Weight, fill = as.factor(Region))) + stat_summary(fun.y= "mean", geom = "bar") 
d

e <- ggplot(nhanes, aes(x = Region, y = Weight, group = Region)) + stat_summary(fun.y= "mean", geom = "bar") +
  geom_point()
e

# People might want to know how to add a confidence interval
f <- ggplot(nhanes, aes(x = Region, y = Weight)) + stat_summary(fun.data = "mean_cl_boot", geom = "pointrange", fun.args=list(conf.int=.999))
f

6: Fitting Lines To The Data

Let’s say we think there might be a linear relationship between height and weight

We can use geom_smooth for this and method = 'lm specifically for a linear model Also level = .95 can specify confidence interval about the estimate at each x value

g <- ggplot(nhanes, aes(x = Height, y = Weight)) + geom_point() + geom_smooth()
g
## `geom_smooth()` using method = 'gam'

Hmm, this actually looks like it’s giving us some pretty bad predictions. We’re not going to get into the stats of this now, but we can also plot using auto which is a mix of models, and might be a bit smarter

h <- ggplot(nhanes, aes(x = Height, y = Weight)) + geom_point() + geom_smooth(method = 'auto', level = .99)
h
## `geom_smooth()` using method = 'gam'

7: Titles, Axis Labels

The labs() command can be added to ggplot with different arguments, lke x, y, or title to make the plots clearer

b + labs(x = 'Urban or Not ', y = 'Height in Inches', title = 'Height by Urban States')

Note, if we want to change labels for FACTORS, not just axes, it’s easier to do that using the tidyverse

require(dplyr)
## Loading required package: dplyr
## Warning: package 'dplyr' was built under R version 3.4.4
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
nhanes$Region <- dplyr::recode(nhanes$Region, '1' = 'Northeast', '2' = 'Midwest', '3' = 'South', '4' = 'West')


b <- ggplot(nhanes, aes(x = Region, y = Weight)) + geom_point() + 
  labs(x = 'Region of US', y = 'Weight (lbs)', title = 'Weight by Region')
b

7: Facetting

Its useful to have several plots in a panel sometimes, not just one.

So for this data set, say we want to plot relationships between height and weight, but by region

We can do this with facet_wrap('Region')

j <- ggplot(nhanes, aes(x = Height, y = Weight)) + geom_point() + geom_smooth(method = 'lm') + facet_wrap('Region')
j

We can even do multiple factors

#Recode Urban

nhanes$Urban <- recode(nhanes$Urban, '1' = 'Metro Area', '2' = 'Non-Metro Area')

#Optional to explain the scales = 'free_x' part

j <- ggplot(nhanes, aes(x = Height, y = Weight)) + geom_point() + geom_smooth(method = 'lm') + facet_wrap(c('Region', 'Urban'), scales = 'free_y')
j

8: Color

We can color either continuously or discretely, depending on how a variable is represented in r

We put col = Height into the aes() function because it’s a grouping factor

Continous Example

b <- ggplot(nhanes, aes(x = Region, y = Weight, col = Height)) + geom_jitter(width = .1) + 
  labs(x = 'Region of US', y = 'Weight (lbs)', title = 'Weight by Region')
b

Discrete Example

h <- ggplot(nhanes, aes(x = Height, y = Weight, col = Region)) + geom_point() 
h

It is EASY to choose your own custom colors (as well as using R presets), but we’re not going to get into that right at this moment

9: Themes

We can use themes to make our plots prettier, and also customize the gridlines a lot

h

h + theme_bw()

b + theme_minimal()

b + theme_void()

b + theme_classic()

h + theme_bw() + labs(title = 'Weight by Height and Region')

10: Saving To Files

Arguments for file , plot, dpi, width, and height

We can use a variety of file formats

ggsave('newPlot22.pdf', plot = h, dpi = 300, width = 5, height = 5)

11: Final Points

Ggplot basic will get you a LONG way.

Also, there is much more ggplot can do for making your plots very pretty, and also plotting lots of complex models

Unlike excel and spss, which can often be cranky and difficult to bend to your will in customizing plots, ggplot is really easy to work with to make your graph look the way you want